{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Enter an Activity Id and See a Summary of that Activity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.display import display, Markdown\n",
    "import snakemd\n",
    "\n",
    "import fitfile\n",
    "from garmindb import GarminConnectConfigManager\n",
    "from garmindb.garmindb import GarminDb, Attributes, Device, ActivitiesDb, Activities, ActivityLaps, ActivityRecords, ActivitiesDevices\n",
    "from idbutils import Location\n",
    "\n",
    "from jupyter_funcs import format_number, format_string, format_temp, format_distance, linked_location\n",
    "from maps import ActivityMap\n",
    "\n",
    "\n",
    "activity_id = input('Enter the id of the activity you would like to display')\n",
    "\n",
    "doc = snakemd.new_doc()\n",
    "\n",
    "gc_config = GarminConnectConfigManager()\n",
    "db_params_dict = gc_config.get_db_params()\n",
    "\n",
    "garmin_db = GarminDb(db_params_dict)\n",
    "garmin_act_db = ActivitiesDb(db_params_dict)\n",
    "measurement_system = Attributes.measurements_type(garmin_db)\n",
    "unit_strings = fitfile.units.unit_strings[measurement_system]\n",
    "distance_units = unit_strings[fitfile.units.UnitTypes.distance_long]\n",
    "altitude_units = unit_strings[fitfile.units.UnitTypes.altitude]\n",
    "temp_units = unit_strings[fitfile.units.UnitTypes.tempurature]\n",
    "\n",
    "activity = Activities.get(garmin_act_db, activity_id)\n",
    "\n",
    "doc.add_heading(activity.name)\n",
    "if activity.description:\n",
    "    doc.add_paragraph(activity.description)\n",
    "\n",
    "doc.add_heading(\"Summary\", 3)\n",
    "start_loc = Location(activity.start_lat , activity.start_long)\n",
    "end_loc = Location(activity.stop_lat , activity.stop_long)\n",
    "doc.add_table(\n",
    "    [\n",
    "        'Start', 'Id', 'Sport', 'Type', f'Distance ({distance_units})', \"Ellapsed Time\", \"Moving Time\", 'Avg HR', 'Max HR', 'Avg RR', 'Max RR', f'Gained ({altitude_units})', f'Lost ({altitude_units})',\n",
    "        \"Calories\", 'Training Load', f\"Avg Temp (deg {temp_units})\", 'Start Location', 'End Location', \"Feel\", \"Effort\"\n",
    "    ],\n",
    "    [\n",
    "        [\n",
    "            activity.start_time, activity_id, activity.sport, activity.type, format_distance(activity.distance), activity.elapsed_time, activity.moving_time, activity.avg_hr, activity.max_hr,\n",
    "            format_number(activity.avg_rr), format_number(activity.max_rr), format_number(activity.ascent), format_number(activity.descent), activity.calories,\n",
    "            format_number(activity.training_load, 1), format_temp(activity.avg_temperature), linked_location(start_loc), linked_location(end_loc), activity.self_eval_feel, activity.self_eval_effort\n",
    "        ]\n",
    "    ]\n",
    ")\n",
    "\n",
    "doc.add_heading(\"Laps\", 3)\n",
    "laps = ActivityLaps.get_activity(garmin_act_db, activity_id)\n",
    "lap_stats = []\n",
    "for lap in laps:\n",
    "    start_loc = Location(lap.start_lat , lap.start_long)\n",
    "    end_loc = Location(lap.stop_lat , lap.stop_long)\n",
    "    lap_stats.append(\n",
    "        [\n",
    "            lap.lap, format_distance(lap.distance), lap.elapsed_time, lap.moving_time, lap.avg_hr, lap.max_hr, format_number(lap.avg_rr), format_number(lap.max_rr),\n",
    "            format_number(lap.ascent), format_number(lap.descent), lap.calories, format_temp(lap.avg_temperature), linked_location(start_loc), linked_location(end_loc)\n",
    "        ]\n",
    "    )\n",
    "doc.add_table(\n",
    "    [\n",
    "        'Lap', f'Distance ({distance_units})', \"Ellapsed Time\", \"Moving Time\", 'Avg HR', 'Max HR', 'Avg RR', 'Max RR', f'Gained ({altitude_units})',\n",
    "        f'Lost ({altitude_units})', \"Calories\", f\"Avg Temp (deg {temp_units})\", 'Start Location', 'End Location'\n",
    "    ],\n",
    "    lap_stats\n",
    ")\n",
    "\n",
    "doc.add_heading(\"Time In Heart Rate Zones\", 3)\n",
    "doc.add_table(\n",
    "    ['Zone', 'HR Threshold', \"Time\"],\n",
    "    [\n",
    "        [\"1\", activity.hrz_1_hr, activity.hrz_1_time],\n",
    "        [\"2\", activity.hrz_2_hr, activity.hrz_2_time],\n",
    "        [\"3\", activity.hrz_3_hr, activity.hrz_3_time],\n",
    "        [\"4\", activity.hrz_4_hr, activity.hrz_4_time],\n",
    "        [\"5\", activity.hrz_5_hr, activity.hrz_5_time],\n",
    "    ])\n",
    "\n",
    "doc.add_heading(\"Training Effect\", 3)\n",
    "doc.add_table(\n",
    "    ['Type', 'Rating'],\n",
    "    [\n",
    "        [\"Aerobic\", activity.training_effect],\n",
    "        [\"Anaerobic\", activity.anaerobic_training_effect],\n",
    "    ])\n",
    "\n",
    "device_entries = ActivitiesDevices.get_activity(garmin_act_db, activity_id)\n",
    "if len(device_entries):\n",
    "    device_rows = []\n",
    "    for device_entry in device_entries:\n",
    "        device = Device.get(garmin_db, device_entry.device_serial_number)\n",
    "        device_rows.append([device.manufacturer.name, device.product, device.device_type, device.serial_number, format_string(device.hardware_version)])\n",
    "    doc.add_heading(\"Devices\", 3)\n",
    "    doc.add_table(['Manufacturer', 'Product', 'Type', 'Serial Number', 'Hardware Version'], device_rows)\n",
    "\n",
    "display(Markdown(str(doc)))\n",
    "\n",
    "if len(laps) and laps[0].start_lat is not None:\n",
    "    records = ActivityRecords.get_activity(garmin_act_db, activity_id)\n",
    "    if len(records) and records[-1].position_lat is not None:\n",
    "        map = ActivityMap(records, laps)\n",
    "        map.display()\n",
    "    else:\n",
    "        print(f\"No record location data in {len(records)} records\")\n",
    "else:\n",
    "    print(\"No lap location data\")\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "d4f50e87ad7f9cd136d9d3dcf547b8236ee2585f92d0ab7c53dfb80e44e3fae9"
  },
  "kernelspec": {
   "display_name": "Python 3.9.5 64-bit ('.venv')",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
